A Simple Calculator using C#


C#

C# is a Microsoft specific programming language. Microsoft decided to make the C# language similar to the C++ language, so that users from C++ could easily learn C#. A program written in C++ is directly executed by the computer. In contrast, a program written in C# is executed by the Platform.NET and is generally slower than a program written in C++. The Platform.NET can be downloaded from Microsoft web site.
C# es un lenguaje específico de Microsoft. Microsoft decidió hacer el lenguaje C# similar al lenguaje C++, de tal forma que los usuarios de C++ pudieran fácilmente aprender C#. Un programa escrito en C++ es ejecutado directamente por la computadora. En cambio, un programa escrito en C# es ejecutado por la Plataforma.NET y es generalmente más lento que un programa en C++. La Plataforma.NET puede ser descargada del sitio web de Microsoft.

Problem 1
Design a calculator to add to numbers using C#.
Diseñe una calculadora para sumar dos números usando C#.

gui

Step 1. Create the proyect.
  1. Open Microsoft Visual Studio.
  2. Select on the menu: File > New > Project ... 
  3. Project types: Visual C# > Windows > Windows Forms Application
  4. Name: CalcSharp
  5. Location: Provide a suitable location for your project
  6. Press the OK button

New

Step 2. Edit the Graphic User Interface (GUI).
  1. Open the "Solution Explorer View" using the menu: View > Solution Explorer
  2. Double click the interface file to edit the GUI.
  3. Open the "Properties View" using the menu: Other Windows View > Properties Window
  4. Use the "Properties View" to edit the properties of the GUI and its elements.
  5. From the toolbox, draw one button, three textboxes and two labels as shown
  6. Set the button "Name" to buttonCalculate and the button "Text" to Calculate
  7. Set the "Name" of the first textbox to textBoxX
  8. Set the "Name" of the second textbox to textBoxY
  9. Set the "Name" of the third textbox to textBoxResult
  10. Set the "Text" of the first label to +
  11. Set the "Text" of the second label to =

Editor

Step 3. Write the code.
  1. Select the button and in the "Properties View" change the tab to "Events"
  2. Double click the "Click" event.
  3. Edit this file by adding the code inside the function: buttonCalculate_Click(object sender, EventArgs e)

Event

Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalcSharp
{     
     public partial class Form1 : Form
     {
          public Form1()
          {
               InitializeComponent();
          }

           private void buttonCalculate_Click(object sender, EventArgs e)
           {
               double x = Convert.ToDouble(this.textBoxX.Text);
                double y = Convert.ToDouble(this.textBoxY.Text);
                double result = x + y;
                this.textBoxResult.Text = result.ToString();
           }
      }
}


Step 4. Run the program.
On the menu: Debug > Start Debugging

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home